<?php defined('BASEPATH') OR exit('No direct script access allowed.');

/**
 * PyroCMS URL Helpers
 *
 * This overrides Codeigniter's helpers/url_helper.php file.
 *
 * @author		Philip Sturgeon
 * @author		PyroCMS Dev Team
 * @package		PyroCMS\Core\Helpers
 */

if (!function_exists('url_title'))
{

	/**
	 * Create URL Title
	 *
	 * Takes a "title" string as input and creates a human-friendly URL string 
	 * with either a dash or an underscore as the word separator. 
	 * Cyrillic alphabet characters are supported.
	 *
	 * @param string $str The string
	 * @param string $separator The separator, dash or underscore.
	 * @param boolean $lowercase Whether it should be converted to lowercase.
	 * @return string The URL slug
	 */
	function url_title($str, $separator = 'dash', $lowercase = FALSE)
	{
		$CI = & get_instance();

		$foreign_characters = array(
			'/ä|æ|ǽ/' => 'ae',
			'/ö|œ/' => 'oe',
			'/ü/' => 'ue',
			'/Ä/' => 'Ae',
			'/Ü/' => 'Ue',
			'/Ö/' => 'Oe',
			'/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ|А/' => 'A',
			'/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª|а/' => 'a',
			'/Б/' => 'B',
			'/б/' => 'b',
			'/Ç|Ć|Ĉ|Ċ|Č|Ц/' => 'C',
			'/ç|ć|ĉ|ċ|č|ц/' => 'c',
			'/Ð|Ď|Đ|Д/' => 'D',
			'/ð|ď|đ|д/' => 'd',
			'/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě|Е|Ё|Э/' => 'E',
			'/è|é|ê|ë|ē|ĕ|ė|ę|ě|е|ё|э/' => 'e',
			'/Ф/' => 'F',
			'/ф/' => 'f',
			'/Ĝ|Ğ|Ġ|Ģ|Г/' => 'G',
			'/ĝ|ğ|ġ|ģ|г/' => 'g',
			'/Ĥ|Ħ|Х/' => 'H',
			'/ĥ|ħ|х/' => 'h',
			'/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ|И/' => 'I',
			'/ì|í|î|ï|ĩ|ī|ĭ|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' => 'Ja',
			'/я/' => 'ja',
			'/Ш/' => 'Sh',
			'/ш/' => 'sh',
			'/Щ/' => 'Shch',
			'/щ/' => 'shch',
			'/Ж/' => 'Zh',
			'/ж/' => 'zh',
		);

		$str = preg_replace(array_keys($foreign_characters), array_values($foreign_characters), $str);

		$replace = ($separator == 'dash') ? '-' : '_';

		$trans = array(
			'&\#\d+?;' => '',
			'&\S+?;' => '',
			'\s+' => $replace,
			'[^a-z0-9\-\._]' => '',
			$replace.'+' => $replace,
			$replace.'$' => $replace,
			'^'.$replace => $replace,
			'\.+$' => ''
		);

		$str = strip_tags($str);

		foreach ($trans as $key => $val)
		{
			$str = preg_replace("#".$key."#i", $val, $str);
		}

		if ($lowercase === TRUE)
		{
			if (function_exists('mb_convert_case'))
			{
				$str = mb_convert_case($str, MB_CASE_LOWER, "UTF-8");
			}
			else
			{
				$str = strtolower($str);
			}
		}

		$str = preg_replace('#[^'.$CI->config->item('permitted_uri_chars').']#i', '', $str);

		return trim(stripslashes($str));
	}

}

if (!function_exists('shorten_url'))
{

	/**
	 * Shorten URL
	 *
	 * Takes a long url and uses the TinyURL API to
	 * return a shortened version.
	 *
	 * Added support for Cyrillic characters.
	 *
	 * @access	public
	 * @param	string	long url
	 * @return	string  short url
	 */
	function shorten_url($url = '')
	{
		$CI = & get_instance();

		$CI->load->spark('curl/1.2.1');

		if (!$url)
		{
			$url = site_url($CI->uri->uri_string());
		}

		// If no a protocol in URL, assume its a CI link
		elseif (!preg_match('!^\w+://! i', $url))
		{
			$url = site_url($url);
		}

		return $CI->curl->simple_get('http://tinyurl.com/api-create.php?url='.$url);
	}

}